package net.mysoft.budgetsoft.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.pagemodel.Mdinfo;
import net.mysoft.budgetsoft.system.service.MdinfoService;
import net.mysoft.framework.authority.AccessType;
import net.mysoft.framework.authority.Permission;
import net.mysoft.framework.base.BaseAction;
import net.mysoft.framework.model.Json;
import net.mysoft.framework.model.TreeNode;
import net.mysoft.framework.util.ExceptionUtil;
import net.mysoft.framework.util.TreeUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2014-04-24 17:59:03 
 */
@Action(value = "mdinfoAction", results = { @Result(name = "mdinfo", location = "/platform/system/mdinfo.jsp"), @Result(name = "mdinfoAdd", location = "/platform/system/mdinfoAdd.jsp"), @Result(name = "mdinfoEdit", location = "/platform/system/mdinfoEdit.jsp"), @Result(name = "mdinfoView", location = "/platform/system/mdinfoView.jsp")})
@Controller("mdinfoAction")
public class MdinfoAction extends BaseAction implements ModelDriven<Mdinfo>{

	private static final Logger logger = Logger.getLogger(MdinfoAction.class);
	@Resource
	private MdinfoService mdinfoService;
	
	private Mdinfo mdinfo = new Mdinfo();
	
	public MdinfoService getMdinfoService(){
		return this.mdinfoService;
	}
	
	public void setMdinfoService(MdinfoService mdinfoService){
		this.mdinfoService = mdinfoService;
	}
	
	public Mdinfo getMdinfo(){
		return this.mdinfo;
	}
	
	public void setMdinfo(Mdinfo mdinfo){
		this.mdinfo = mdinfo;
	}
	
	public Mdinfo getModel() {
		return mdinfo;
	}
	
	@Permission(access=AccessType.LOGIN)
	public void tree() {
		try {
			
			List<Mdinfo> list = mdinfoService.find(mdinfo);
			List<TreeNode> tree = new ArrayList<TreeNode>();
			for(Mdinfo mdinfo : list) {
				TreeNode node = new TreeNode();
				node.setId(mdinfo.getC_mdcode());
				node.setParentId(mdinfo.getShangJiCDBH() != null ? mdinfo.getShangJiCDBH() : "root");
				node.setText(mdinfo.getC_cname());
				node.setIsLeaf(mdinfo.getIdLeaf());
				node.setUrl("center.jsp");
				Map<String, Object> attributes = new HashMap<String, Object>();
				attributes.put("url",mdinfo.getUrl());//"/budgetInitEditAction!loadMain.action");// mdinfo.getC_helpurl());
				//attributes.put("url","http://localhost:8080/budgetsoft/platform/supreport/supcanTest.jsp");
				attributes.put("isLeaf",mdinfo.getIdLeaf());
				node.setAttributes(attributes);
				tree.add(node);
			}
			super.writeJson(TreeUtil.generateTree(tree, new ArrayList(), mdinfo.getRootID()!= null ? mdinfo.getRootID() : "root"));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}
	
	/**
	 * Mdinfo列表页面
	 * @return
	 */
	public String mdinfo() {
		return "mdinfo";
	}
	
	/**
	 * Mdinfo查看页面
	 * @return
	 */
	public String mdinfoView() {
		return "mdinfoView";
	}
	
	/**
	 * Mdinfo添加页面
	 * @return
	 */
	public String mdinfoAdd() {
		return "mdinfoAdd";
	}
	
	/**
	 * Mdinfo编辑页面
	 * @return
	 */
	public String mdinfoEdit() {
		return "mdinfoEdit";
	}
	
	/**
	 * 添加
	 */
	public void add() {
		Json j = new Json();
		try {
			mdinfoService.add(mdinfo);
			j.setSuccess(true);
			j.setMsg("添加成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 修改
	 */
	public void edit() {
		Json j = new Json();
		try {
			mdinfoService.edit(mdinfo);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 删除
	 */
	public void delete() {
		Json j = new Json();
		try {
			mdinfoService.delete(mdinfo);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 列表
	 */
	public void datagrid() {
		try {
			super.writeJson(mdinfoService.datagrid(mdinfo));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}
}
